var ZYRequestClient = require("../../../../utils/zy-request");
var requestClient;
const app = getApp();
Page({
  data:{
    electronicFenceList:[],
    navbarData:{
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '电子围栏', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     height: app.globalData.height * 2 + 20
  },
  onLoad() {
    requestClient = new ZYRequestClient();
  },
  // 加载电子围栏列表
  loadElectronicFenceList(){
    var _this = this;
    var defaultCompany = wx.getStorageSync('defaultCompany');
    if(defaultCompany == ''){
      defaultCompany = { id:0 };
    }
    requestClient.getElectronicFenceList(defaultCompany.id,function(res){
      for(var i=0; i < res.data.data.length; i++){
        var sealNameArray =[];
        for(var j=0;j<res.data.data[i].sealVoList.length;j++){
            var seal = res.data.data[i].sealVoList[j];
            sealNameArray.push(seal.name);
        }
        if(sealNameArray.length > 0){
          res.data.data[i].sealNames = sealNameArray.join(',');
        } else {
          res.data.data[i].sealNames = "无";
        }
      }
      _this.setData({
        electronicFenceList:res.data.data
      });
    });
  },
  onShow(){
    var _this = this;
    _this.setData({
      electronicFenceList:[]
    });
    requestClient.checkFunctionPermission(1005,'您没有权限设置电子围栏，请联系企业创建者！',function(){
      _this.loadElectronicFenceList();
    },function(){
      wx.navigateBack({
        delta: 1,
      })
    });
  },
  // 点击添加电子围栏
  bindAddClick(e){
    requestClient.checkFunctionPermission(1005,'您没有权限添加电子围栏，请联系企业创建者！',function(){
      wx.navigateTo({
        url: '../addelectronicfence/index',
      })
    },function(){
      wx.navigateBack({
        delta: 1,
      })
    });
  },
  // 点击电子围栏
  bindElectronicFenceClick(e){
    var _this = this;
    var electronicFence = e.currentTarget.dataset.electronic;
    wx.showActionSheet({
      itemList: ['删除'],
      itemColor:'#ff0000',
      success(e){
        if(e.tapIndex == 0){
          wx.showModal({
            title:'删除电子围栏',
            content:'您确定要删除吗',
            success(r){
              if(r.confirm){
                requestClient.checkFunctionPermission(1005,'您没有权限删除电子围栏，请联系企业创建者！',function(){
                  requestClient.deleteElectronicFence(electronicFence.id,function(res){
                    _this.loadElectronicFenceList();
                  })
                },function(){
                  wx.navigateBack({
                    delta: 1,
                  })
                });
              }
            }
          })
        }
      }
    })
  }
})
